function saveImageToPhotosAlbum(option) {
  return new Promise((resolve, reject) => {
    wx.saveImageToPhotosAlbum({
      ...option,
      success: resolve,
      fail: reject,
    })
  })
}
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img:"",
    maskey: true,
    list:[],
    page:0,
    pagesize:10,
    
  },
  // 取消遮罩层
  mask_key: function () {
    this.setData({
      maskey: true
    })
  },
  // 出现遮罩层
  hasmask: function (e) {
    var that = this
    var id = that.data.id
    app.util.request({
      'url': 'entry/wxapp/Zixun.generate',
      data: {
        id: id
      },
      success: function (res) {
        console.log(res)
        that.setData({
          img: res.data
        })
        // console.log(that.data.img)
      },
      fail: function (err) {
        console.log(err)
      },
    })
    this.setData({
      maskey: false
    })
  },
  // 下载图片
  maskbtn: function () {
    // 保存到相册
    let that = this
    const imageFile = that.data.img
    wx.showLoading({
      title: '图片保存中...'
    })
    wx.downloadFile({
      url: imageFile,
      success: function (res) {
        // 图片保存到本地
        console.log(res.tempFilePath)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.hideLoading()
            wx.showModal({
              title: '图片已存入相册',
              content: '好东西要分享，发给朋友们看看。',
              showCancel: false
            })
          },
          fail: function (err) {
            console.log(err)
            if (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied' || err.errMsg === 'saveImageToPhotosAlbum:fail auth deny' || err.errMsg === 'saveImageToPhotosAlbum:fail authorize no response') {
              console.log('22222222222222222222222222222222=============================')
              // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
              wx.showModal({
                title: '提示',
                content: '需要您授权保存相册',
                showCancel: false,
                success: modalSuccess => {
                  wx.openSetting({
                    success(settingdata) {
                      console.log('settingdata', settingdata)
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限成功,再次点击按钮即可保存',
                          showCancel: false
                        })
                      } else {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限失败，将无法保存到相册！',
                          showCancel: false
                        })
                      }
                    },
                    fail(failData) {
                      console.log('failData', failData)
                    },
                    complete(finishData) {
                      console.log('finishData', finishData)
                    }
                  })
                }
              })
            }
          },
          complete(res) {
            wx.hideLoading()
          }
        })
      }
    })
  },






  navicate() {
    var that = this
    wx.reLaunch({
      url: '/hyb_yl/tabBar/index/index',
    })
  },
  listitem(e){
    let tit = e.currentTarget.dataset.title;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/details/details?title='+tit+'&id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = options.type;
    var that = this;
    that.setData({
      type:type
    })
    if(type == '1')
    {
      that.getzhengzhuan();
    }else{
      that.getList();
    }
    that.getAdv();
    app.util.request({
      'url': 'entry/wxapp/index.base',
      success: function (res) {
        var se = res.data.ztcolor
        
        wx.setNavigationBarTitle({
          title: res.data.show_title
        })
        wx.setNavigationBarColor({
          frontColor: '#ffffff',
          backgroundColor: se,
        })
        wx.setStorage({
          key: 'color',
          data: se,
        })
      },
      fail: function (err) {
        console.log(err)
      },
    })
    
  },
  getAdv:function(){
    var that = this;
    app.util.request({
      'url':"emtry/wxapp/answer.adv",
      data:{
        position:that.data.type,
      },
      success:function(res){
        that.setData({
          adv:res.data
        })
      }
    })
  },
  // 获取症状
  getzhengzhuan:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/answer.zhengzhuan_type",
      success:function(res){
        that.setData({
          info:res.data
        })
      }
    })
  },
  // 获取症状列表
  listitems:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/itemdetails/itemdetails?id='+id,
    })
  },
  // 获取信息列表
  getList:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/answer.tanks",
      data:{
        type:that.data.type,
        page:that.data.page,
        pagesize:that.data.pagesize
      },
      success:function(res){
        var page = that.data.page + 1;
        that.setData({
          list: that.data.list.concat(res.data),
          page: page
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var keyword = that.data.keyword;
    that.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})